﻿using UnityEngine;

namespace TC
{
    public class SimpleParallel : BTNode, IRepeat, IPopChild
    {
        public FlowOutput<Leaf> action;
        public FlowOutput<FlowNode> childTree;

        private int tmpStatus;
        private int count;

        public override void Execute(Dispatcher chain)
        {
            base.Execute(chain);
            
            count = 0;
            chain.Call(action);
            chain.Call(childTree);
        }

        public void Next(int status)
        {
#if UNITY_EDITOR
            Debug.Assert(count > 0 && count < 2);
#endif

            ++count;
            if (count == 2)
            {
                if (tmpStatus == 0 && status == 0)
                {
                    chain.End(0);
                }
                else
                {
                    chain.End(1);
                }
            }
            else if (count == 1)
            {
                tmpStatus = status;
            }
        }
    }
}
